package com.errands.cn.services;

import com.errands.cn.entity.Evaluate;
import com.errands.cn.entity.User;
import com.errands.cn.mapper.EvaluateMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class EvaluateService {
    @Autowired
    private EvaluateMapper evaluateMapper;
    @Autowired
    private UserService userService;

    @Transactional
    public boolean save(Evaluate evaluate){
        try {
            evaluateMapper.insert(evaluate);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    @Transactional
    public boolean deleteEvaluate(Integer id){
        try {
            evaluateMapper.deleteByPrimaryKey(id);
            return true;
        }catch (Exception e){
            return false;
        }
    }

    public List<Evaluate> getByEvaluate(Integer evaluateId){
        List<Evaluate> list = evaluateMapper.selectByEvaluate(evaluateId);
        for (int i=0;i<list.size();i++){
            User user = userService.getUser(list.get(i).getAcceptUid());
            list.get(i).setAcceptUser(user.getName());
        }
        return list;
    }
    public List<Evaluate> getByAccept(Integer acceptId){
        List<Evaluate> list = evaluateMapper.selectByAccept(acceptId);
        for (int i=0;i<list.size();i++){
            User user = userService.getUser(list.get(i).getEvaluateUid());
            list.get(i).setEvaluateUser(user.getName());
        }
        return list;
    }

}
